vim9script

# Fibonacci function in Vim9 script (recursive)
def FibonacciRecursive(n: number): number
    if n <= 1
        return n
    else
        return FibonacciRecursive(n - 1) + FibonacciRecursive(n - 2)
    endif
enddef

# Fibonacci function in Vim9 script (iterative)
def FibonacciIterative(n: number): number
    if n <= 1
        return n
    endif

    var a = 0
    var b = 1
    for i in range(2, n)
        var c = a + b
        a = b
        b = c
    endfor
    return b
enddef

var num = 30
echo "Vim9 Script Fibonacci(" .. num .. ")"
# 测试递归实现
var start_time = reltime()
var fib_recursive = FibonacciRecursive(num)
echo "Recursive result: " .. fib_recursive
var end_time = reltime(start_time)
echo "Recursive execution time: " .. reltimestr(end_time) .. " seconds"
# 测试迭代实现
start_time = reltime()
var fib_iterative = FibonacciIterative(num)
echo "Iterative result: " .. fib_iterative
end_time = reltime(start_time)
echo "Iterative execution time: " .. reltimestr(end_time) .. " seconds"